package com.zeroxml.cms.service.impl;

import com.zeroxml.cms.service.IScrapeService;
import com.zeroxml.cms.service.ICatalogService;
import com.zeroxml.cms.service.IArticleService;
import com.zeroxml.cms.bo.Catalog;
import com.zeroxml.cms.bo.Article;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.io.IOUtils;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;

/**
 * User: cjp
 * Date: 2007-9-30
 * Time: 19:35:05
 */
public class ScrapeService implements IScrapeService {
    private ICatalogService catalogService;

    private IArticleService articleService;


    public void setCatalogService(ICatalogService catalogService) {
        this.catalogService = catalogService;
    }


    public void setArticleService(IArticleService articleService) {
        this.articleService = articleService;
    }

    @Transactional
    public void scrapeCatalogs() {
        String url = "http://www.open-open.com/";
        Matcher matcher = parse(url, "<a href='(.*?)'><img src='image/right.gif' border='0' width='15' height='15' />&nbsp;(.*?)</a>");
        while (matcher.find()) {
            String dd = matcher.group(1);
            String ss = matcher.group(2);
            String url1 = url + dd;
            Catalog catalog = new Catalog();
            catalog.setName(ss);
            Catalog parentCatalog = catalogService.getCatalog("openSource");
            catalog.setParentCatalog(parentCatalog);
            catalog.setStatus(Catalog.STATUS_NORMAL);
            catalog.setOrders(1);
            catalogService.createCatalog(catalog);
            String pattern = "<img src='image/item.gif' width='13' height='14' />&nbsp;<a href='(.*?)'>(.*?)</a>";
            String patternPage = "<a href='(.*?)'><span class='' id='sp2'><strong>(.*?)</strong></span></a>";
            Matcher matcherPage = parse(url1, patternPage);
            Matcher matcher1 = parse(url1, pattern);

            if (matcherPage == null) {
                while (matcher1.find()) {
                    String articleHref = matcher1.group(1);
                    String articleName = matcher1.group(2);

                    //System.out.println(articleHref);
                    // System.out.println(articleName);
                    Matcher matcher2 = parse(url + articleHref, "<span class=\"style7\">(.*?)\r\n(.*?)</span>");
                    while (matcher2.find()) {
                        String ar = matcher2.group(1);
//                    String fromUrl=matcher2.group(4);
                        Article article = new Article();
                        article.setTitle(articleName);
                        article.setContent(ar);
                        article.setCreateTime(new Date());
                        article.setCatalog(catalog);
                        article.setFromSite(url);
//                    article.setFromUrl(fromUrl);
                        article.setFromType(Article.FROM_OTHER);
                        article.setStatus(Article.STATUS_WAITCHECK);
                        article.setSubject(ar);
                        articleService.createArticle(article);
                        // System.out.println(ar);
                    }
                }
            } else {
                List<String> pageList = new ArrayList();
                pageList.add(0, url1);
                int i = 1;
                while (matcherPage.find()) {
                    pageList.add(i, url + matcherPage.group(1));
                    i = i + 1;
                }
                for (int j = 0; j < pageList.size(); j++) {
                    matcher1 = parse(pageList.get(j), pattern);
                    while (matcher1.find()) {
                        String articleHref = matcher1.group(1);
                        String articleName = matcher1.group(2);
                        Matcher matcher2 = parse(url + articleHref, "<span class=\"style7\">(.*?)\r\n(.*?)</span>");
                        while (matcher2.find()) {
                            String ar = matcher2.group(1);
                            Article article = new Article();
                            article.setTitle(articleName);
                            article.setContent(ar);
                            article.setCreateTime(new Date());
                            article.setCatalog(catalog);
                            article.setFromSite(url);
                            article.setFromType(Article.FROM_OTHER);
                            article.setStatus(Article.STATUS_WAITCHECK);
                            article.setSubject(ar);
                            articleService.createArticle(article);
                        }
                    }
                }
            }

        }

    }

    public void scapeArticles(long catalogId) {

    }


    public Matcher parse(String url, String regex) {
        HttpClient httpClient = new HttpClient();
        GetMethod get = new GetMethod(url);
        try {
            httpClient.executeMethod(get);
            String resStr = IOUtils.toString(get.getResponseBodyAsStream(), "gb2312");
//            String resStr = get.getResponseBodyAsString();
            Pattern ap = Pattern.compile(regex);
            Matcher matcher = ap.matcher(resStr);
            return matcher;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            get.releaseConnection();
        }
        return null;
    }
}
